<div class="modal j_modalPath">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close"><span>×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">设置技术路径</h4>
            </div>
            <div class="modal-body" style="max-height: 400px;">
                <div class="crm-tree" id="crmTreePathMenu">
                    <input class="form-control" id="crmTreePathFilter" type="text" placeholder="输入关键字进行检索" style="float: none">
                    <ul>
                        @forelse($techRouteList as $key=>$value)
                            <li class="parent"><label><input type="checkbox" value="{{$value['id']}}" data-domain="{{$value['domain']}}"><span>{{$value['name']}}</span></label>
                            </li>
                        @empty
                        @endforelse
                    </ul>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>
<script>
    (function ($) {
        $(function () {
            var $m = $('.j_modalPath');
            var _crmTreePathMenu = $('#crmTreePathMenu');
            initPathTree();
            function initPathTree() {
                //初始化是否有子菜单
                _crmTreePathMenu.find('li').each(function () {
                    var _t = $(this);
                    if (_t.children('ul').length > 0) {
                        _t.addClass('parent');
                    }
                });
                //初始化选中状态
                var ids = $('#j_techPathIds').val();
                if (ids) {
                    ids = ',' + ids.toString() + ',';
                }
                _crmTreePathMenu.find('input').each(function () {
                    var _t = $(this);
                    if ((ids).indexOf(',' + _t.val() + ',') != -1) {
                        _t.prop('checked', true);
                    }
                });
            }

            //确认
            $m.find(".btn").click(function () {
                var $btn = $(this);
                var _chk = _crmTreePathMenu.find('input[type="checkbox"]:checked');
                if (_chk.length === 0) {
                    //模拟失败操作
                    //$.Alert({obj: $btn, txt: '至少选择一个技术路径！', type: 'danger'});
                    //return false;
                    $(".modal").parent().fadeOut(200);
                } else {
                    //成功操作
                    var i = 0, techHtml = '', techIds = ',';
                    for (; i < _chk.length; i++) {
                        techIds += _chk.eq(i).val() + ',';
                        techHtml += '<tr><td align="center">' + _chk.eq(i).next().html() + '<input class="j_techPathId" type="hidden" value="' + _chk.eq(i).val() + '"></td>';
                        techHtml += '<td align="center"><input type="checkbox"></td>';
                        techHtml += '<td align="center"><input type="checkbox"></td>';
                        techHtml += '<td align="center"><input type="checkbox"></td>';
                        techHtml += '<td align="center"><input type="checkbox"></td>';
                        techHtml += '<td align="center"><input type="checkbox"></td>';
                        techHtml += '<td align="center"><input type="checkbox"></td></tr>';
                    }
                    $.Alert({
                        obj: $btn, txt: '设置成功！', callback: function () {
                            $("#j_alert").remove();
                            $m.parent().fadeOut(200);
                            $('#j_techPathIds').val(techIds);
                            $(".j_techService").css('display', 'inline-block').find('tbody').html(techHtml);
                        }
                    });
                }
            });

            //点击切换子菜单显示
            _crmTreePathMenu.find('li').click(function (e) {
                e.stopPropagation();
                var _t = $(this);
                _t.toggleClass('toggle').children('ul').toggle();
            });

            //点击文字显示右侧内容
            _crmTreePathMenu.find('label').click(function (e) {
                e.stopPropagation();
            });
            _crmTreePathMenu.find('input').click(function (e) {
                e.stopPropagation();
                var _t = $(this);
                var _p = _t.parent('label');
                var id = _t.val();
                if (_t.prop('checked')) {
                    _p.parents('ul').prev().find('input[type="checkbox"]').prop('checked', true);
                } else {
                    _p.next().find('input[type="checkbox"]').removeProp('checked');
                }
            });

            //输入框快速定位
            var startSearch = true;
            $('#crmTreePathFilter').bind('change keyup blur', function () {
                if (startSearch) {
                    startSearch = false;
                    setTimeout(treeSearhPath, 500);
                }
            });
            function treeSearhPath() {
                var key = $('#crmTreePathFilter').val();
                _crmTreePathMenu.find('li').css('display', 'none');
                _crmTreePathMenu.find('label').each(function () {
                    var _t = $(this);
                    if (_t.html().indexOf(key.toLowerCase()) >= 0) {
                        _t.parents('li').css('display', 'block');
                    }
                });
                startSearch = true;
            }
        });
    })(jQuery);
</script>
